/**********************************************************************************
// Level 1 Source File
// 
// Creation:	04 Jun 2011
// Updated:		05 Ago 2011
// Compiler:	Visual Studio 2010
//
// Notes:		Level 1 of Space Race
//
**********************************************************************************/

#include "SpaceRace.h"

/**********************************************************************************/

void SpaceRace::Level1Init()
{
	// reset the viewport to the center of the world
	playerVpBox.left   = viewport.left   = 0.0f + (GetGameWorldWidth() - GetWindowWidth())/2;
	playerVpBox.top    = viewport.top    = 0.0f + (GetGameWorldHeight() - GetWindowHeight())/2;
	playerVpBox.right  = viewport.right  = viewport.left + (GetWindowWidth()-1);
	playerVpBox.bottom = viewport.bottom = viewport.top + (GetWindowHeight()-1);

	/*****************************************/

	levelbar->MoveTo(viewport.left + GetWindowWidth()/2.0f, viewport.top + 150.0f);
	helpbar->MoveTo(viewport.left + GetWindowWidth()/2.0f, viewport.bottom - 85.0f);
	keymapbar->MoveTo(viewport.left + GetWindowWidth()/2.0f, viewport.bottom - 15.0f);

	/*****************************************/
	
	// reset space ship to its initial state
	spaceShip->Reset();
	spaceShip->Animate(INERTIA);
	spaceShip->MoveTo(GetMidGameWorldX(), GetMidGameWorldY());

	// register ship to scene manager
	scene.RegisterMoving(*spaceShip);

	// create Station One sprite
	stationOne = new Sprite("Resources/StationOne.png");
	stationOne->customType = objFixed;
	Point vetStationOne[6] = {Point(-60,-90),Point(-60,40),Point(60,40),Point(60,-90),Point(300,100),Point(-300,100) };
	stationOne->boundingbox = new Poly(vetStationOne, 6);
	stationOne->boundingbox->color =  Color(128,128,128);
	stationOne->boundingbox->customType = objFixed;
	stationOne->MoveTo(float(GetMidGameWorldX()),float(GetMidGameWorldY()));
	fixed.push_back(stationOne);
	scene.RegisterStatic(*stationOne);

	// initialize finish marker
	finPosX = GetMidGameWorldX();
	finPosY = GetMidGameWorldY()+230;
	finishBB = Rect(-42, -12, 41, 11);
	finishBB.MoveTo(finPosX,finPosY);
	finishBB.color = Color(128,128,128);
	finishBB.customType = objFinish;
	scene.RegisterStatic(finishBB);

	finish[0]->MoveTo(float(finPosX),float(finPosY));
	finish[1]->MoveTo(float(finPosX),float(finPosY));
	finish[2]->MoveTo(float(finPosX),float(finPosY));
	
	// level time limit
	timeLimit = 10;
	elapsedTime = 0;

	// erase player trace
	ZeroMemory(&playerTrace, sizeof(playerTrace));
	dotsNum = 0;

	// initial finish color is green
	finishColor = Color(0,255,0);
	finishColorIndex = GREEN;
	viewAchievements = false;
	inspectResult = false;

	// initialize achievements
	achievements->pedal = true;
	ZeroMemory(achievements->redundantR, sizeof(achievements->redundantR));
	ZeroMemory(achievements->redundantL, sizeof(achievements->redundantL));
}

/**********************************************************************************/

void SpaceRace::Level1CleanUp()
{
	// unregister space ship
	scene.UnRegisterMoving(*spaceShip);

	delete stationOne->boundingbox;

	// clean fixed game objects
	for (ifix = fixed.begin(); ifix != fixed.end(); ++ifix)
	{
		scene.UnRegisterStatic(*((Sprite *) *ifix));
		delete (*ifix);
	}
	fixed.clear();

	// clean missiles
	for (imis = missiles.begin(); imis != missiles.end(); ++imis)
	{
		scene.UnRegisterMoving(*((Geometry *) *imis));
		delete *imis;			
	}
	missiles.clear();

	scene.UnRegisterStatic(finishBB);
}

/**********************************************************************************/